<template>
  <div class="login">
    <div class="login_ct">
      <h4>登录账号</h4>
      <p>虾米 - 严选商城欢迎您</p>
      <my-input v-model="mobile" :class="reMobile==0?'':reMobile==1?'success':'error'" placeholder="请输入手机号" @blur="mobileFtn">
        <div class="icon">
          <b v-if="reMobile==2" style="color:red">必填</b>
          <i class="iconfont icon-shouji"></i>
        </div>
      </my-input>
      <my-input v-model="pwd" :type="showPwd?'text':'password'" placeholder="请输入密码" :class="rePwd==0?'':rePwd==1?'success':'error'" @blur="pwdFtn">
        <div class="icon" @click="showPwd=!showPwd">
          <i :class="['iconfont',showPwd?'icon-zhengyan':'icon-biyan']"></i>
        </div>
      </my-input>
      <button @click="login">登录</button>
      <span>忘记密码</span>
      <a href="">还没有注册？ 立即注册</a>
    </div>
  </div>
</template>

<script>
import { Notify } from 'vant';
import { reactive, toRefs } from "vue";
import { getToken } from '../../api/second/second';
import myInput from "../../components/myInput.vue";
import { useRouter } from 'vue-router';
export default {
  components: { myInput },
  setup() {
    const router = useRouter();
    const data = reactive({
      mobile:'',
      pwd:'',
      reMobile: 0 , // 0 就代表，没有填写过，1代表成功，2代表失败
      rePwd: 0, // 0 就代表，没有填写过，1代表成功，2代表失败
      showPwd: false, // 是否显示密码的状态
    })
    const mobileFtn = () =>{ // 手机号的验证
      let re = /^1[3-9][0-9]{9}$/;
      data.reMobile = re.test(data.mobile)?1:2;
    }
    const pwdFtn = () => { // 密码的验证
       data.rePwd = data.pwd?1:2
    }
    const login = () => {
       if (data.reMobile == 1 && data.rePwd == 1){
          getToken({mobile:data.mobile,pwd:data.pwd}).then(res => {
            if (res.code == 0){
              localStorage.setItem('token',res.data.token);
              router.back();
            }
          })
       } else {
          Notify({ type: 'warning', message: '请填写用户或者密码' });
       }
    }
    return{
        ...toRefs(data),
        mobileFtn,
        pwdFtn,
        login
    }
  },
};
</script>
<style lang="scss" scoped>
.login {
  padding: 40px;
  .login_ct {
    padding: 0 60px;
    border: 2px solid #f5f5f5;
    text-align: center;
    h4 {
      margin-top: 60px;
      color: #666;
    }
    p {
      margin-top: 40px;
      color: #909090;
      font-size: 32px;
    }
    button {
      color: #fff;
      font-size: 30px;
      width: 420px;
      height: 100px;
      border: none;
      border-radius: 10px;
      margin-top: 40px;
      background: linear-gradient(135deg, #e570e7 0%, #79f1fc 100%);
    }
    span {
      font-size: 24px;
      color: #999;
      display: block;
      margin-top: 40px;
    }
    a {
      display: block;
      font-size: 32px;
      color: #549ff9;
      margin: 40px 0 300px;
    }
    .error{
      border-color: rgb(225, 44, 30);
      background: rgb(255, 233, 228);
    }
    .success{
      border-color: rgb(56, 219, 36);
      background: rgb(225, 255, 223);
    }
  }
}
.icon {
  font-size: 24px;
  .iconfont {
    font-size: 36px;
    margin-left: 10px;
  }
}
</style>